package com.sizu.web;


import com.alibaba.fastjson.JSON;
import com.sizu.common.dto.PurchaseListDto;
import com.sizu.common.dto.ReturnListDto;
import com.sizu.common.saledto.TCustomerReturnListDto;
import com.sizu.common.saledto.TSaleListDto;
import com.sizu.entity.TCustomerStatement;
import com.sizu.entity.TPurchaseStatement;
import com.sizu.entity.TSaleStatement;
import com.sizu.entity.TSupplierStatement;
import com.sizu.service.ITCustomerStatementService;
import com.sizu.service.ITPurchaseStatementService;
import com.sizu.service.ITSaleStatementService;
import com.sizu.service.ITSupplierStatementService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/statement")
public class SyncStatement {

    @Autowired
    private ITCustomerStatementService itCustomerStatementService;

    @Autowired
    private ITSupplierStatementService itSupplierStatementService;

    @Autowired
    private ITPurchaseStatementService itPurchaseStatementService;

    @Autowired
    private ITSaleStatementService itSaleStatementService;

    /**
     * 进货同步
     * @param purchaseListDto
     */
    @RequestMapping("/purchase")
    public void syncPurchase(@RequestBody PurchaseListDto purchaseListDto){
        TSupplierStatement tSupplierStatement=new TSupplierStatement();
        BeanUtils.copyProperties(purchaseListDto,tSupplierStatement);
        tSupplierStatement.setSupplierListId(purchaseListDto.getId());
        tSupplierStatement.setSupplierDate(purchaseListDto.getPurchaseDate());
        tSupplierStatement.setSupplierNumber(purchaseListDto.getPurchaseNumber());
        tSupplierStatement.setStyle("购入");
        itSupplierStatementService.insert(tSupplierStatement);
        if(null!=purchaseListDto.getPurchaseListGoodsJSON()&&!"".equals(purchaseListDto.getPurchaseListGoodsJSON())){
            List<TPurchaseStatement> tPurchaseStatements = JSON.parseArray(purchaseListDto.getPurchaseListGoodsJSON(), TPurchaseStatement.class);
            for (TPurchaseStatement tPurchaseStatement : tPurchaseStatements) {
                tPurchaseStatement.setSupplierId(tSupplierStatement.getId());
                itPurchaseStatementService.insert(tPurchaseStatement);
            }
        }
    }

    /**
     * 退购同步
     * @param returnListDto
     */
    @RequestMapping("/return")
    public void syncReturn(@RequestBody ReturnListDto returnListDto){
        TSupplierStatement tSupplierStatement=new TSupplierStatement();
        BeanUtils.copyProperties(returnListDto,tSupplierStatement);
        tSupplierStatement.setSupplierListId(returnListDto.getId());
        tSupplierStatement.setSupplierDate(returnListDto.getReturnDate());
        tSupplierStatement.setSupplierNumber(returnListDto.getReturnNumber());
        tSupplierStatement.setStyle("退购");
        itSupplierStatementService.insert(tSupplierStatement);
        if(null!=returnListDto.getReturnListGoodsJSON()&&!"".equals(returnListDto.getReturnListGoodsJSON())){
            List<TPurchaseStatement> tPurchaseStatements = JSON.parseArray(returnListDto.getReturnListGoodsJSON(), TPurchaseStatement.class);
            for (TPurchaseStatement tPurchaseStatement : tPurchaseStatements) {
                tPurchaseStatement.setSupplierId(tSupplierStatement.getId());
                itPurchaseStatementService.insert(tPurchaseStatement);
            }
        }
    }

    /**
     * 出售同步
     * @param tSaleListDto
     */
    @RequestMapping("/sale")
    public void syncSale(@RequestBody TSaleListDto tSaleListDto){
        TCustomerStatement tCustomerStatement=new TCustomerStatement();
        BeanUtils.copyProperties(tSaleListDto,tCustomerStatement);
        tCustomerStatement.setCustomerListId(tSaleListDto.getId());
        tCustomerStatement.setCustomerDate(tSaleListDto.getSaleDate());
        tCustomerStatement.setCustomerNumber(tSaleListDto.getSaleNumber());
        tCustomerStatement.setStyle("出售");
        itCustomerStatementService.insert(tCustomerStatement);
        if(null!=tSaleListDto.getSaleListGoodsJson()&&!"".equals(tSaleListDto.getSaleListGoodsJson())){
            List<TSaleStatement> tSaleStatements = JSON.parseArray(tSaleListDto.getSaleListGoodsJson(), TSaleStatement.class);
            for (TSaleStatement tSaleStatement : tSaleStatements) {
                tSaleStatement.setSaleListId(tCustomerStatement.getId());
                itSaleStatementService.insert(tSaleStatement);
            }
        }
    }

    /**
     * 退货同步
     * @param tCustomerReturnListDto
     */
    @RequestMapping("/customerreturn")
    public void syncCustomerreturn(@RequestBody TCustomerReturnListDto tCustomerReturnListDto){
        TCustomerStatement tCustomerStatement=new TCustomerStatement();
        BeanUtils.copyProperties(tCustomerReturnListDto,tCustomerStatement);
        tCustomerStatement.setCustomerListId(tCustomerReturnListDto.getId());
        tCustomerStatement.setCustomerDate(tCustomerReturnListDto.getCustomerReturnDate());
        tCustomerStatement.setCustomerNumber(tCustomerReturnListDto.getCustomerReturnNumber());
        tCustomerStatement.setStyle("退货");
        itCustomerStatementService.insert(tCustomerStatement);
        if(null!=tCustomerReturnListDto.getTCustomerReturnListGoodsJson()&&!"".equals(tCustomerReturnListDto.getTCustomerReturnListGoodsJson())){
            List<TSaleStatement> tSaleStatements = JSON.parseArray(tCustomerReturnListDto.getTCustomerReturnListGoodsJson(), TSaleStatement.class);
            for (TSaleStatement tSaleStatement : tSaleStatements) {
                tSaleStatement.setSaleListId(tCustomerStatement.getId());
                itSaleStatementService.insert(tSaleStatement);
            }
        }
    }

}
